<?php
/**
 * Created by PhpStorm.
 * User: 雨寒
 * Date: 2018/12/13
 * Time: 下午15:45
 */

namespace app\admin\controller;

use think\Exception;
use think\Db;
use app\common\model\Message;

class Withdraw extends Common
{
    //用户提现
    public function userWithdraw()
    {
        $this->templateTitle('用户提现列表');
        //判断条件
        $where['uw.id'] = ['gt', 0];

        //搜索条件-用户
        $user_condition = request()->param("user_condition");
        $this->assign("user_condition", $user_condition);
        if ($user_condition) {
            $where["us.nick_name|us.mobile|uw.user_name"] = $user_condition;
        }

        //提现状态
        if (!isset($_REQUEST['status'])) {
            $status = 100;
        } else {
            $status = request()->param('status');
        }

        if ($status != '100') {
            $where['uw.status'] = $status;
        }
        $this->assign('status', $status);
        //搜索条件-提现时间
        if (request()->has("time_range")) {
            //解析时间
            $timeRange = request()->param("time_range");
            $timeRange = explode(" ~ ", $timeRange);
            //模板渲染值
            $this->assign("range_start", ["time_range" => $timeRange[0]]);
            $this->assign("range_end", ["time_range" => $timeRange[1]]);

            $where['uw.create_time'] = ["BETWEEN", [strtotime($timeRange[0] . " 00:00:00"), strtotime($timeRange[1] . " 23:59:59")]];

        } else {
            //模板渲染值
            $this->assign("range_start", ["time_range" => '2018-01-01']);
            $this->assign("range_end", ["time_range" => date("Y-m-d")]);
        }

        //获取提现列表
        $list = Db::name("user_withdraw")
            ->alias('uw')
            ->join('__USER__ us', 'us.id=uw.uid', 'LEFT')
            ->where($where)
            ->order("uw.id DESC")
            ->field('uw.*,us.nick_name,us.mobile')
            ->paginate(10);
        /*累计金额*/
        $all_money = 0;
        $data_list = $list->toArray()['data'];
        $this->assign("all_money", $all_money);
        $this->assign("list", $list);

        return $this->fetch();
    }

    /**
     * 审核通过
     */
    public function checkok()
    {
        $uw_id = input("id");
        $uw = \db("user_withdraw")->where("id", $uw_id)->where("status", 0)->find();
        if (empty($uw)) {
            $this->ajaxError("记录不存在");
        } else {
            \db("user_withdraw")->where("id", $uw_id)->where("status", 0)->update([
                "status" => 1
            ]);
            $this->ajaxSuccess("审核成功");
        }

    }

    /*提现申请通过*/
    public function confirmpay()
    {
        if (request()->isAjax()) {
            $id = request()->param('id');
            $info = Db::name('user_withdraw')
                ->where('id', $id)
                ->find();
            if (!$info || $info['status'] != 1) {
                return $this->ajaxError('无此提现记录或者提现已处理');
            }

            $data = [
                'update_time' => time(),
                'status' => 2
            ];

            $save = Db::name('user_withdraw')
                ->where('id', $id)
                ->update($data);

            if ($save) {
                \db("user")->where("id", $info["uid"])->setInc("commission_ok", $info["money"]);
                /*操作记录*/
                Message::admin_log(0, "后台通过提现申请！id:" . $id);
                return $this->ajaxSuccess('确认打款成功');
            } else {
                return $this->ajaxError('服务器繁忙请稍后再试');
            }

        }
    }

    public function trash()
    {
        if (request()->isAjax()) {
            $id = request()->param('id');
            $info = Db::name('user_withdraw')
                ->where('id', $id)
                ->find();
            if (!$info || $info['status'] != 0) {
                return $this->ajaxError('无此提现记录或者提现已处理禁止重复处理');
            }

            $data = [
                'update_time' => now_datetime(),
                'status' => 3
            ];

            $save = Db::name('user_withdraw')
                ->where('id', $id)
                ->update($data);

            if ($save) {
                return $this->ajaxSuccess('提现作废成功');
            } else {
                return $this->ajaxError('服务器繁忙请稍后再试');
            }

        }
    }

}